<?php 

class SystemController extends Controller {
    public $type = __CLASS__;
    
    public function initialise() {
        
    }
    
    public function login($args = null) {
        if (Session::inst()->getUserid())
        {
            $this->CMM->registerControllerIntoChain("home", "main");
            return;
        }
        else
        {
            if (isset($_POST["password"]))
            {
                $session = Session::inst();
                $session->writeCookies($_POST["username"], $_POST["password"]);
                if (!$session->authenticateSession())
                {
                    $session->deleteCookies();
                    $this->view->registerTemplate("system/notification");
                    $this->view->registerVariable("Incorrect username or password", "notification");
                    $this->view->registerVariable("red", "colour");
                    $this->view->registerTemplate("system/login");
                }
                else
                {
                    //header("Location: " . URL("home", "main"));
                    header('Refresh: 3; url='.URL("home", "main"));
                    $this->view->registerTemplate("system/notification");
                    $this->view->registerVariable("You have successfully logged in<br/>Your browser will redirect you in 3 seconds.<br/><br/>Click <a href=\"".URL("home", "main")."\">Here</a> if your browser does not refresh.", "notification");
                    $this->CMM->registerControllerIntoChain("home", "main");
                }
            }
            else
            {
                $this->view->registerTemplate("system/login");
            }
        }
    }
    
    public function logout($args = null) {
        $session = Session::inst();
        if (! $session->getUserid())
        {
            $this->view->registerTemplate("system/notification");
            $this->view->registerVariable("You are not logged in.", "notification");
            $this->CMM->registerControllerIntoChain("home", "main");
        }
        else
        {
            $session->deleteCookies();
            $this->view->registerTemplate("system/notification");
            $this->view->registerVariable("You have successfully logged out.", "notification");
            $this->CMM->registerControllerIntoChain("home", "main");
        }
    }
    
    public function createRootAccount($args = null) {
        $user = new User();
        $user->username = "root";
        $user->password = RC4Encryption::inst()->encrypt("root");
        $user->create();
        
        DBConnection::inst()->query("INSERT INTO UserRoleNames SET userid=".$user->id.", roleid=7");
    }
    
    public function home($args = null){
        $this->CMM->registerControllerIntoChain("System", "noFound");
    }
    
    public function notFound() {
        $this->view->registerTemplate('system/systemMessage');
        $this->view->registerVariable('Error', 'messageType');
        $this->view->registerVariable('Page Not Found', 'messageTitle');
        $this->view->registerVariable(
            'The page you requested could not be found. Please try searching or returning to the home page.',
            'messageText'
        );
        return true;
    }
}

?>